﻿using DevExpress.Xpo.DB.Helpers;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm_Mes.WorkingManagement.TeamSet
{
    public partial class AddTeamSetForm : DevExpress.XtraEditors.XtraForm
    {
        public delegate void AddTeamSetDelegate();
        public event AddTeamSetDelegate AddTeamSetEvent;
        public AddTeamSetForm()
        {
            InitializeComponent();
            InitTeamTypeComboBox();
        }

        private void panelControl2_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            string TeamTypeId = "";
            if (comboBoxEdit1.SelectedItem is ComboBoxItem selectedItem)
            {
                TeamTypeId = selectedItem.Value;
            }
            var data = JsonConvert.SerializeObject(new
                {

                teamSetCode = textEdit1.Text,
                teamTypeId=TeamTypeId,
                teamSetName = textEdit2.Text,
                teamLeader = textEdit3.Text,
                remark = memoEdit1.Text,
                });
            var response = await HttpClientHelper.RequestUrl(RequestType.post, MethodGolab.POST_SETCREATE, data);
            var resultData = JsonConvert.DeserializeObject<TeamSetResponse<TeamSetModel>>(response);

            if (resultData.code == 200)
            {
                AddTeamSetEvent.Invoke();
                this.Close();
            }
            else
            {
                XtraMessageBox.Show($"添加团队设置失败：{resultData.message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        // 初始化团队类型下拉框数据
        private async void InitTeamTypeComboBox()
        {
            try
            {
                // 初始化下拉框显示格式
                comboBoxEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;

                // 获取团队类型列表
                var teamTypeList = await GetAllTeamTypes();
                if (teamTypeList != null && teamTypeList.Count > 0)
                {
                    // 设置数据源，使用Dictionary存储Id和Name
                    var dataSource = new Dictionary<string, string>();
                    dataSource.Add("", "请选择"); // 添加一个空选项

                    foreach (var item in teamTypeList)
                    {
                        dataSource.Add(item.id.ToString(), item.teamTypeName);
                    }

                    // 绑定到下拉框
                    comboBoxEdit1.Properties.Items.Clear();
                    foreach (var item in dataSource)
                    {
                        comboBoxEdit1.Properties.Items.Add(new ComboBoxItem { Value = item.Key, Display = item.Value });
                    }

                    // 默认选中全部
                    comboBoxEdit1.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show($"加载团队类型数据失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 获取所有团队类型数据
        private async Task<List<TeamTypeItem>> GetAllTeamTypes()
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    var response = await HttpClientHelper.RequestUrl(RequestType.get, MethodGolab.SHOW_DEFECT + "?PageIndex=1&PageSize=1000");
                    //序列化
                    var result = JsonConvert.DeserializeObject<TeamTypeResponse<TeamTypeItem>>(response);
                    if (result.code == 200)
                    {
                        return result.items;
                    }
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show($"获取团队类型列表失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return new List<TeamTypeItem>();
            }
        }

    }
}